package com.jackrain.nea.fc.ps.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.ps.api.ViewCSkuToJDQueryCmd;
import com.jackrain.nea.fc.ps.mapper.ViewCProDistribToJDMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

/**
 * @author wuwenxue
 * @date 2018/05/05
 **/
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "ps")
public class ViewCSkuToJDQueryCmdImpl extends CommandAdapter implements ViewCSkuToJDQueryCmd {

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {

        ValueHolder vh = new ValueHolder();
        ViewCProDistribToJDMapper viewCProDistribToJDMapper = ApplicationContextHandle.
                getBean(ViewCProDistribToJDMapper.class);
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"),
                "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        log.info("ViewCSkuToJDQueryCmdImpl--param-->{}",param);

        if (null == param || (!param.containsKey("IDS")) || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }

        Object[] id = param.getJSONArray("IDS").toArray();
        JSONArray jsonArray = new JSONArray();
        List<HashMap> list = viewCProDistribToJDMapper.findSku(id);

        for (HashMap<String, Object> proskuMap : list) {

            String skuEcode = TypeUtils.castToString(proskuMap.get("ECODE"));
            proskuMap.put("SKULIST", skuEcode);
            StringBuffer standardSb = new StringBuffer();
            if (null != skuEcode) {
                //商品规格
                Long spec3obj = TypeUtils.castToLong(proskuMap.get("PS_C_SPEC3OBJ_ID"));
                if (null != spec3obj) {
                    String spec3objName = viewCProDistribToJDMapper.selSpecobj(spec3obj);
                    standardSb.append(spec3objName);
                }
                //商品颜色
                Long spec1obj = TypeUtils.castToLong(proskuMap.get("PS_C_SPEC1OBJ_ID"));
                if (null != spec1obj) {
                    String spec1objName = viewCProDistribToJDMapper.selSpecobj(spec1obj);
                    standardSb.append(spec1objName);
                }
                //商品尺码
                Long spec2obj = TypeUtils.castToLong(proskuMap.get("PS_C_SPEC2OBJ_ID"));
                if (null != spec2obj) {
                    String spec2objName = viewCProDistribToJDMapper.selSpecobj(spec2obj);
                    standardSb.append(spec2objName);
                }
            }
            proskuMap.put("STANDARDLIST", standardSb);

            //ID 商品ID
            Long proID = TypeUtils.castToLong(proskuMap.get("PS_C_PRO_ID"));
            Object[] objID = new Object[]{proID};
            List<HashMap> prolist = viewCProDistribToJDMapper.findAllPro(objID);
            for (HashMap hashMap : prolist) {
                //商品三级分类编码 
                Long largeClassID = TypeUtils.castToLong(hashMap.get("LARGECLASS"));
                if (null != largeClassID) {
                    String eName = viewCProDistribToJDMapper.selLargeClassEname(largeClassID);
                    proskuMap.put("LARGECLASS", eName);
                }

                //品牌名称
                Long brandID = TypeUtils.castToLong(hashMap.get("PS_C_BRAND_ID"));
                if (null != brandID) {
                    String eName = viewCProDistribToJDMapper.selBrandEname(brandID);
                    proskuMap.put("PS_C_BRAND_ID", eName);
                }

                //生产厂商
                Long supplierID = TypeUtils.castToLong(hashMap.get("CP_C_SUPPLIER_ID"));
                if (null != supplierID) {
                    String eName = viewCProDistribToJDMapper.selSupplierEname(supplierID);
                    proskuMap.put("CP_C_SUPPLIER_ID", eName);
                }

                //配销中心编码
                Long distribID = TypeUtils.castToLong(hashMap.get("CP_C_DISTRIB_ID"));
                if (null != distribID) {
                    String ecode = viewCProDistribToJDMapper.findByDistrib(distribID);
                    proskuMap.put("CP_C_DISTRIB_ID", ecode);
                }

                proskuMap.put("ECODE", hashMap.get("ECODE"));
                proskuMap.put("ENAME", hashMap.get("ENAME"));
                proskuMap.put("PRICELIST", hashMap.get("PRICELIST"));

            }

            JSONObject js = new JSONObject(proskuMap);
            JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(js,
                    "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            jsonArray.add(param1);
        }

        vh.put("data", jsonArray);

        return vh;
    }
}
